'''
Case Type： 故障&可靠性测试
Case Name： 数据库备节点多台虚机同时进行切换（switchover）操作；
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.备数据库节点并行switchover；
'''
import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import *
from testcase.utils.ComThread import ComThread
from testcase.utils.Logger import Logger


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case156.py start------------')
        self.rootNode = Node()
        self.dbUserNode1 = Node(node='PrimaryDbUser')
        self.dbUserNode = Node(node='PrimaryRoot')
        self.dbUserNode2 = Node(node='Standby1Root')
        self.dbUserNode3 = Node(node='Standby1DbUser')
        self.sh = CommonSH('Standby1DbUser')
        self.Constant = Constant()

    def test_startdb(self):
        self.log.info('----------------------------启线程1执行switchover-----------------------------')
        switch_thread_1 = ComThread(self.sh.executeGsctl,
                                    args=('switchover', self.Constant.SWITCH_SUCCESS_MSG,))
        switch_thread_1.setDaemon(True)
        switch_thread_1.start()

        self.log.info('----------------------------启线程2执行switchover-----------------------------')
        switch_thread_2 = ComThread(self.sh.executeGsctl,
                                    args=('switchover', self.Constant.SWITCH_SUCCESS_MSG,))
        switch_thread_2.setDaemon(True)
        switch_thread_2.start()

        switch_thread_1.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        switch_result_1 = switch_thread_1.get_result()
        self.log.info(switch_result_1)

        switch_thread_2.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        switch_result_2 = switch_thread_2.get_result()
        self.log.info(switch_result_2)
        result_msg = [switch_result_1, switch_result_2]

        self.log.info(type(result_msg))
        self.log.info(result_msg)
        self.assertTrue(result_msg.count(False) < 2)

    def tearDown(self):
        time.sleep(10)
        cmd = "cat /etc/hostname"
        primary_hostname = self.dbUserNode.sh(cmd).result()
        self.log.info(primary_hostname)
        if self.commonshpri.check_whether_need_switch(primary_hostname):
            self.log.info('执行swithover,还原主备')
            switch_msg = self.commonshpri.executeGsctl('switchover', self.Constant.SWITCH_SUCCESS_MSG)
            self.assertTrue(switch_msg)

            self.log.info('执行refresh')
            refresh_cmd = f'source {macro.DB_ENV_PATH};gs_om -t refreshconf'
            msg = self.dbUserNode1.sh(refresh_cmd).result()
            self.assertIn(self.Constant.REFRESH_SUCCESS_MSG, msg)
        self.log.info(
            '------------------------------------reliability_dbsys156.py finish------------------------------------')
